package org.test.hadoop.mapreduce;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.WritableComparable;

public class SplitWordWritable implements WritableComparable<SplitWordWritable> {

	private String value;
	
	public SplitWordWritable(){
		
	}
	
	public SplitWordWritable(String value){
		this.value = value;
	}
	
	public void write(DataOutput out) throws IOException {
		out.writeUTF(value);
	}

	public void readFields(DataInput in) throws IOException {
		value = in.readUTF();
	}

	public int compareTo(SplitWordWritable o) {
		String thisValue = this.value;
	    String thatValue = o.value;
	    String thisNum = thisValue.split("	")[1];
	    String thatNum = thatValue.split("	")[1];
	    int thisCount = Integer.parseInt(thisNum);
	    int thatCount = Integer.parseInt(thatNum);
	    return -(thisCount<thatCount ? -1 : (thisCount==thatCount ? 0 : 1));
	}

	@Override
	public int hashCode() {
		return this.value.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (value == obj) {
		    return true;
		}
		if(value == null&&obj== null){
			return true;
		}
		if(value == null && obj != null){
			return false;
		}
		if(value !=null&&obj == null){
			return false;
		}
		return this.value.equals(obj.toString());
	}

	@Override
	public String toString() {
		return this.value;
	}
}
